<?php

/***  DOCUMENTATION LAYER

Klenwell DBO Unit Test

Last Update: Aug 2007
  
DESCRIPTION
  this file provides a template for other simple unit tests.
  
NOTES
  $test_root dynamically sets root in order to keep tests working after
  they are moved to the __REPOSITORY directory
    
______________________________________________________________________________*/

$_tx0 = microtime();
$test_root = ( strpos(dirname(__FILE__), 'repository') === false ) ? '' : '../';


// Test Name
$test_name = 'create kdbo test db';

// Include Files
require_once($test_root . 'class.kdbo.php');
require_once(dirname($_SERVER['DOCUMENT_ROOT']) . '/__DEV/kdbo.mysql_cred.inc.php');   // my db connection info is here

// Configure Test
$debug = 1;

// styling functions
function p1($bg='#fff') { print "<pre style='padding:1em; background:{$bg}; border:4px solid #ccc;'>"; }
function p2() { print '</pre>'; }

// *** Test Procedure

// Connect
p1('#fff');

// connect to mysql host without selecting db?
if ( 1 )
{
  p1('#eef');
  require_once($test_root . '../php_ceo/ext_oss/adodb_lite/adodb.inc.php');
  $_du = urlencode($DB['user']);
  $_dp = urlencode($DB['pass']);
  $_dsn = "mysql://{$_du}:{$_dp}@{$DB['host']}/";
  if ( !$Dbo =& ADONewConnection($_dsn) ) trigger_error('unable to connect to database [' . $this->db_name . ']', E_USER_WARNING);
  else print "<h3 style='color:green;'>we should be connected!</h3>";
  p2();
  
  // recreate database
  p1('#eef');
  $sql = <<<SQL
CREATE TABLE IF NOT EXISTS `test` 
(
	/* Columns */
  `uid` int(11) NOT NULL auto_increment,
  `varchar` varchar(40) NOT NULL default '',
  `int` int(11) NOT NULL default '0',
  `tiny_int` tinyint(4) NOT NULL default '0',
  `double` double(10,2) NOT NULL default '0.00',
  `text` text,
  `datetime` datetime NOT NULL default '0000-00-00 00:00:00',
  `uts` int(11) NOT NULL default '0',	

	/* Indices */
  PRIMARY KEY  (`uid`),
	INDEX (`varchar`),
  UNIQUE (`uts`)
) 
TYPE=MyISAM 
AUTO_INCREMENT=1 ;
SQL;

  print '<p>recreating db and table</p>';
  $Dbo->Execute('DROP DATABASE IF EXISTS `kdbo`');
  $Dbo->Execute('CREATE DATABASE IF NOT EXISTS `kdbo` ;');
  $Dbo->Execute('USE `kdbo`');
  $Dbo->Execute('DROP TABLE IF EXISTS `test`');
  if ( $Dbo->Execute($sql) ) print '<p>db and table recreated</p>';
  p2();
}
p2();



// Footer
$_txn = microtime();
$stx = number_format(((substr($_txn,0,9)) + (substr($_txn,-10)) - (substr($_tx0,0,9)) - (substr($_tx0,-10))),4); 
echo "<h6>test '$test_name' complete in $stx s</h6>";
?>
